<template>
  <div class="my-container">
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image round class="avatar" :src="userInfo.photo" fit="cover" />
          <span>{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <div class="count">{{ userInfo.art_count }}</div>
          <div class="text">头条</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.follow_count }}</div>
          <div class="text">关注</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.fans_count }}</div>
          <div class="text">粉丝</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.like_count }}</div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>
    <div class="header no-login" v-else>
      <img
        class="mobile-img"
        src="~@/assets/mobile.png"
        @click="$router.push('/login')"
      />
      <span class="text">登录 / 注册</span>
    </div>

    <!-- 宫格导航 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-lishi"> </i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 消息通知，小智同学，退出登录 -->
    <van-cell value="消息通知" is-link />
    <van-cell value="小智同学" is-link class="md9" />
    <van-cell value="退出登录" v-if="user">
      <van-button
        type="primary"
        block
        color="linear-gradient(to right, #ff6034, #ee0a24)"
        @click="onLogout"
        clickable
        >退出登录</van-button
      >
    </van-cell>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { getUserInfo } from '@/api/user';
export default {
  name: 'MyPage',
  components: {},
  props: {},
  data () {
    return {
      userInfo: {}
    };
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    if (this.user) {
      this.loadUser();
    }
  },
  mounted () {},
  methods: {
    onLogout () {
      this.$dialog
        .confirm({
          title: '确认',
          message: '确认退出吗？'
        })
        .then(() => {
          this.$store.commit('setUser', null);
          // on confirm
        })
        .catch(() => {
          // on cancel
        });
    },
    async loadUser () {
      try {
        console.log('ok');
        const { data } = await getUserInfo();
        this.userInfo = data.data;
      } catch (error) {
        console.log(error);
        this.$toast('获取数据失败');
      }
    }
  }
};
</script>

<style scoped lang='less'>
.my-container {
  > .header {
    height: 361px;
    background: url("~@/assets/banner.png") no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    .mobile-img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    .text {
      color: #fff;
      font-size: 28px;
    }
  }
  .user-info {
    .base-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        span {
          color: #fff;
          font-size: 3px;
        }
        .avatar {
          width: 132px;
          height: 132px;
          margin-right: 23px;
          border: 3px solid #fff;
        }
      }
    }
    .data-stats {
      width: 100%;
      display: flex;
      .data-item {
        flex: 1;
        height: 130px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    .grid-item {
      height: 141px;
      i.toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #f96153;
      }
      .toutiao-lishi {
        color: #ffa01d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
  .md9 {
    margin-bottom: 10px;
  }
}
</style>
